// subpages0/liverPrediction/liverPrediction.js
let livePlayer;
var app = getApp();
Page({
  data: {
    hours: '00',
    minutes: '00',
    seconds: '00',
    useLivePlugin:true
  },
  onLoad: function (e) {
    var that = this;
    if (e) {
      if (e.scene) {
        var scene = decodeURIComponent(e.scene).split('&');
        console.log(scene);
        var secneObj = {};
        for (var i = 0; i < scene.length; i++) {
          var arr = scene[i].split('=');
          var key = arr[0];
          secneObj[key] = arr[1];
        }
        e = secneObj;
      }
    };
    if(e&&e.roomId){
      that.setData({
        roomId: e.roomId
      })
    }
    if (!app.globalData.plumSession) {
      //回调
      app.plumSessionCallback = plumSession => {
        if (plumSession != '') {
          console.log('查询到登录信息');
          that.getLiverInfo();
        } else {
          console.log('未查询到登录信息');
          app.wechatSq(that);
        }
      }
    } else {
      that.getLiverInfo();
    }
  },
  onShow:function(){
    var that = this;
  },
  onUnload:function(){
    var that = this;
    clearInterval(that.data.intervarID);
  },
  getLiverInfo: function () {
    var that = this;
    wx.showLoading({
      title: '加载中',
      mask:true
    })
    wx.request({
      url: app.globalData.liverqurl,
      data: {
        map: 'applet_appletlive_room_info',
        room_id:that.data.roomId
      },
      success: function (res) {
        console.log(res.data);
        if(res.data.ec==200){
          livePlayer = requirePlugin('live-player-plugin');
          var allData = res.data.data;
          //页面顶部时间展示处理
          var start_time = allData.start_time;
          var time1 = start_time.trim().split(" ")[0];
          time1 = time1.substr((time1.indexOf("/") + 1));
          console.log(time1);
          var time2 = start_time.trim().split(" ")[1];

          livePlayer.getLiveStatus({ room_id: allData.room_id }).then(res => {
            if (allData.live_status != res.liveStatus) {
              allData.live_status = res.liveStatus;
              app.updateLiveStatus(allData.room_id, res.liveStatus)
            }
          }).catch(err => {
            console.log(err)
          })

          that.setData({
            roomInfo: allData,
            time1: time1,
            time2: time2
          });

          if (allData.room_name) {
            app.setNavtitle(allData.room_name);
          }

          that.countDjs();
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  //开播倒计时
  countDjs: function () {
    var that = this;
    var livertime = new Date(that.data.roomInfo.start_time);
    livertime = parseInt(livertime.getTime() / 1000);
    var curTime = parseInt(new Date().getTime() / 1000);
    var liverspan = parseInt(livertime-curTime);
    if (liverspan>0){
      that.data.intervarID = setInterval(function () {
        liverspan--;
        that.djs(liverspan);
      }, 1000)
    }

  },
  djs: function (leftTime) {
    var that = this;
    that.setData({
      leftTime: leftTime
    })
    if (leftTime > 0) {
      var days = parseInt(leftTime / 60 / 60 / 24, 10);
      var hours = parseInt(leftTime / 60 / 60 % 24, 10);
      var minutes = parseInt(leftTime / 60 % 60, 10);
      var seconds = parseInt(leftTime % 60, 10);
      hours = hours < 10 ? '0' + hours : hours;
      minutes = minutes < 10 ? '0' + minutes : minutes;
      seconds = seconds < 10 ? '0' + seconds : seconds;
      that.setData({
        hours: hours,
        minutes: minutes,
        seconds: seconds
      })
      if (hours == '00' && minutes == '00' && seconds == '00') {
        clearInterval(that.data.intervarID);
      }
    } else {
      that.setData({
        hours: '00',
        minutes: '00',
        seconds: '00',
      })
    }
  },
  toGoodDetail:function(e){
    var that = this;
    var url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: "/"+url,
    })
  },
  onPullDownRefresh: function () {
    var that = this;
    that.onLoad();
  },
  showSharemodal() {
    var pageShare = this.selectComponent('.page-share'),
      posterData = { map: 'applet_appletlive_live_poster', id: this.data.roomId};
    console.log(posterData)
    this.setData({
      posterData: posterData
    })
    pageShare.showSharemodal()
  },
  hideSharemodal() {
    var pageShare = this.selectComponent('.page-share');
    pageShare.hideSharemodal()
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this;
    var roomInfo = that.data.roomInfo;
    var title = roomInfo.room_name;
    var cover = roomInfo.anchor_img ? roomInfo.anchor_img:'';
    return {
      title: title,
      imageUrl:cover,
      path: '/subpages0/liverPrediction/liverPrediction?roomId=' + roomInfo.room_id,
      success: function () {

      }
    }
  }
})